package com.int_tech.control.controller;

import com.int_tech.utils.DateTool;
import com.int_tech.utils.HttpClient;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.net.URLEncoder;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by liubochao on 2018/7/24.
 */
public class TestPostJD {


    public static void  main(String[]args) throws Exception {
        // test2();
//        test2();
        int c = (int) (899999999*Math.random()+100000000);

        System.out.print(c);
    }
    public static void test2() throws Exception {
        String app_key ="f17fa61cbd37476cbe63961d09e70912";
        String app_secret ="edb0caf7ab274079be48495671351252";
        String token ="15c7dd0f50f14fd69cd59cf3a2053470";

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("vehicleNumber","010AEV890");
        jsonObject.put("type",2);
        jsonObject.put("dataSource","23");
        jsonObject.put("lat","116.544664");
        jsonObject.put("lng","39.808138");
        jsonObject.put("vehicleLong",9.4);
        jsonObject.put("vehicleWidth",2.5);
        jsonObject.put("vehicleHeight",2.4);
        jsonObject.put("volume",100.0);

        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("position","1-1");
        jsonObject2.put("volume",21.0);

        JSONArray jsonArray1 = new JSONArray();

        jsonArray1.add(jsonObject2);

        jsonObject.put("vehicleBoxDetailDtoList",jsonArray1.toString());
        jsonObject.put("gatherTime", "1532425235938");
        jsonObject.put("businessCode","82059");
        jsonObject.put("rowNumber",7);
        jsonObject.put("colNumber",2);

        String sss = "jdtms";

        JSONArray jsonArray = new JSONArray();

        jsonArray.add(jsonObject);
        jsonArray.add(sss);


        String param_json = jsonArray.toString();
        String method = "jingdong.test.receiveVehicleBox";
        String version ="2";
        SimpleDateFormat format0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timestamp = format0.format(new Date());
        String sign = buildSign(timestamp, version, method, param_json, token, app_key, app_secret);
        System.out.println(sign);
        timestamp = URLEncoder.encode(timestamp, "utf-8");
        String url = "http://test-gateway.jdwl.com/routerjson?access_token="+token+"&app_key="+app_key+"&method="+method+"&sign="+sign+"&timestamp="+timestamp+"&v="+version+"";
        System.out.println(url);
        System.out.println(param_json);
        String response = HttpClient.post(url, param_json);
        System.out.println(response);
    }

    private static String buildSign(String timestamp, String version, String method ,

                                    String paramJson , String accessToken ,

                                    String appKey, String appSecret) throws Exception {

        //第一步，按照顺序填充参数

        Map<String, String> map = new TreeMap();

        map.put("timestamp", timestamp);

        map.put("v", version);

        map.put("method", method);

        map.put("param_json", paramJson);

        map.put("access_token", accessToken);

        map.put("app_key", appKey);



        StringBuilder sb = new StringBuilder(appSecret);

        //按照规则拼成字符串

        for (Map.Entry entry : map.entrySet()) {

            String name = (String) entry.getKey();

            String value = (String) entry.getValue();

            sb.append(name).append(value);

        }

        sb.append(appSecret);

        //MD5

        return md5(sb.toString());

    }

    public static String md5(String source) throws Exception {

        MessageDigest md = MessageDigest.getInstance("MD5");

        byte[] bytes = md.digest(source.getBytes("utf-8"));

        return byte2hex(bytes);
    }

    private static String byte2hex(byte[] bytes) {

        StringBuilder sign = new StringBuilder();

        for (int i = 0; i < bytes.length; i++) {

            String hex = Integer.toHexString(bytes[i] & 0xFF);

            if (hex.length() == 1) {

                sign.append("0");

            }
            sign.append(hex.toUpperCase());
        }

        return sign.toString();
    }
}
